(ns com.clojurebook.url-shortener
  (:use [compojure.core :only (GET PUT POST defroutes)])
  (:require (compojure handler route)
            [ring.util.response :as response]
            [hiccup.core :as h]))


(defroutes app*
  (GET "/" request "Welcome!")

  (GET ["/user/:id" :id #"[0-9]+"] [id url] (str "number:" url id))
  (GET "/user/:id" [id url] (str url id))
  (GET "/user/" [] (str "user"))
  (GET "/form" [] 
       (h/html [:html [:header [:title "hello"]]
                [:body 
                 [:form {:action "/form-submit" :method "post"} 
                  [:input {:type "text" :name "a"}]
                  [:br]
                  [:input {:type "submit" :value "提交"}]]]]))
  (POST "/form-submit" [a] 
        (h/html [:html [:header [:title "hello"]]
                 [:body
                  (str a)
                  [:a {:href "/form"} "返回"]]]))
  (compojure.route/not-found "Sorry, there's nothing here."))

(def app (compojure.handler/api app*))

;; ; To run locally:
(use '[ring.adapter.jetty :only (run-jetty)]) 
(def server (run-jetty #'app {:port 8080 :join? false}))

;; http://localhost:8080/user/12.2/hello 不能匹配

